package copyengine.scenes.viewport.impl
{
	import copyengine.scenes.viewport.ICEViewport;
	import copyengine.scenes.viewport.ICEViewportListener;
	import copyengine.utils.GeneralUtils;

	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;

	public class CEViewport implements ICEViewport
	{
		private var viewportContainer:Sprite;

		private var viewportListenerVector:Vector.<ICEViewportListener>;

		private var prePosX:int;
		private var prePosY:int;

		private var posX:int;
		private var posY:int;

		private var width:int;
		private var height:int;

		protected var isViewportMoved:Boolean = false;

		public function CEViewport()
		{
			viewportContainer = new Sprite();
			viewportListenerVector = new Vector.<ICEViewportListener>();
		}

		public function moveTo(_posX:int, _posY:int):void
		{
			isViewportMoved = true;
			posX = _posX;
			posY = _posY;
		}

		public final function updateListener():void
		{
			for each (var listener:ICEViewportListener in viewportListenerVector)
			{
				if (isViewportMoved)
				{
					listener.viewportMoveUpdate(posX, posY, prePosX, prePosY, viewportWidth, viewportHeight);
				}
				else
				{
					listener.viewportNotMoveUpdate(posX, posY, viewportWidth, viewportHeight);
				}
			}
			isViewportMoved = false;
			prePosX = posX;
			prePosY = posY;
		}

		public final function onScreenResize(_currenStageWidth:int,
											 _currentStageHeight:int,
											 _offestWidth:int,
											 _offestHeight:int,
											 _offsetXPercent:Number,
											 _offsetYPercent:Number):void
		{
			width = _currenStageWidth;
			height = _currentStageHeight;
			for each (var listener:ICEViewportListener in viewportListenerVector)
			{
				listener.viewportMoveUpdate(posX, posY, prePosX, prePosY, viewportWidth, viewportHeight);
			}
		}

		public final function addListener(_listener:ICEViewportListener):void
		{
			viewportListenerVector.push(_listener);
		}

		public final function removeListener(_listener:ICEViewportListener):void
		{
			for (var i:int = 0; i < viewportListenerVector.length; i++)
			{
				if (_listener == viewportListenerVector[i])
				{
					viewportListenerVector.splice(i, 1);
					break;
				}
			}
		}

		public final function initialize(_posX:int, _posY:int, _width:int, _height:int):void
		{
			posX = _posX;
			posY = _posY;
			width = _width;
			height = _height;

			viewportContainer.graphics.beginFill(0, 0);
			viewportContainer.graphics.drawRect(0, 0, width, height);
			viewportContainer.graphics.endFill();
		}

		public final function start():void
		{
			for each (var listener:ICEViewportListener in viewportListenerVector)
			{
				listener.viewportInitialized(posX, posY, width, height);
			}
		}

		public final function dispose():void
		{
			GeneralUtils.removeTargetFromParent(viewportContainer);
			viewportContainer = null;
			viewportListenerVector = null;
		}

		public final function get container():DisplayObjectContainer
		{
			return viewportContainer;
		}

		public final function get viewportWidth():int
		{
			return width;
		}

		public final function get viewportHeight():int
		{
			return height;
		}

		public final function get currentPosX():int
		{
			return posX;
		}

		public final function get currentPosY():int
		{
			return posY;
		}
	}
}
